﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using EIIS.Infrastructure.Service.System;
using EIIS.Infrastructure.Model.System;
using EIIS.Presentation.Controllers.CommonSupport;
using EIIS.Presentation.Controllers.ViewModel;
using EIIS.Common.Jsons;
using EIIS.FrameWork.Attributes;

namespace EIIS.Presentation.Controllers.System
{
    [Description("数据字典条目管理")]
    public class DictionaryController:BaseController
    {
        public IDicCategoryManager DicCategoryManager { get; set; }
        public IDictionaryManager DictionaryManager { get; set; }

        [Description("数据字典条目管理页面")]
        [ViewPage]
        public ActionResult Index()
        {            
            return View();
        }

        public ActionResult LoadAllDicCategory()
        {
            List<DicCategory> list = DicCategoryManager.LoadAll().OrderBy(c=>c.Index).ToList();
            DicCategory curDicCategory = list.Where(d => d.Level == 0).First();
            TreeData curTreeData = ConvertToTreeData(curDicCategory);
            CreateTree(curTreeData, curDicCategory, list);

            List<TreeData> treeList = new List<TreeData>();
            treeList.Add(curTreeData);

            return Json(treeList);
        }

        public ActionResult LoadDictionarysByDicCategoryId(string id,int page, int rows, string order, string sort)
        {
            long total = 0;
            IList<Dictionary> ilist = DictionaryManager.LoadAllByPage(out total, id, page, rows, order, sort); 
            return this.JsonFormat(new GridData { total = total, rows = ilist.ToList() });

        }

        [Description("新增数据字典条目")]
        [Function(ButtonID="btnAdd")]
        public ActionResult Add(string id)
        {
            Dictionary entity = null;
            MessageInfo msg = new MessageInfo();

            if (string.IsNullOrEmpty(id))//分类编号为空
            {
                msg.IsSuccess = false;
                msg.Message = "新增失败，分类编号不能为空";
                return Json(msg);
            }
            DicCategory dic = DicCategoryManager.Get(id);
            entity = entity ?? new Dictionary
            {
                Category = dic
            };
            this.ViewData["entity"] = entity;
            return View();
        }

        [Description("修改数据字典条目")]
        [Function(ButtonID = "btnUpdate")]
        public ActionResult Edit(string id)
        {
            MessageInfo msg = new MessageInfo();
            if (string.IsNullOrEmpty(id))
            {
                msg.IsSuccess = false;
                msg.Message = "修改失败，条目编号不能为空";
                return Json(msg);
            }
            Dictionary entity = DictionaryManager.Get(id);
            this.ViewData["entity"] = entity;

            return View();
        }

        [Description("删除数据字典条目")]
        [Function(ButtonID = "btnDelete")]
        public ActionResult Delete(IList<string> idList)
        {
            MessageInfo msg = new MessageInfo();
            msg.IsSuccess = true;
            msg.Message = "删除成功";
            DictionaryManager.Delete(idList.Cast<object>().ToList());
            return Json(msg);
        }

        [Description("保存数据字典条目")]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Save(Dictionary entity)
        {
            string strCID = Request["entity.Category.Id"];

            if (string.IsNullOrEmpty(entity.Id))//新增
            {
                entity.Id = Guid.NewGuid().ToString();
                entity.Category = DicCategoryManager.Get(strCID);
                this.DictionaryManager.Save(entity);
            }
            else
            {
                var model = DictionaryManager.Get(entity.Id);
                model.Name = entity.Name;
                model.Code = entity.Code;
                model.Index = entity.Index;
                model.InputCode1 = entity.InputCode1;
                model.InputCode2 = entity.InputCode2;
                model.InputCode3 = entity.InputCode3;
                model.Description = entity.Description;
                model.Category = DicCategoryManager.Get(strCID);
                DictionaryManager.Update(model);
            }
            MessageInfo msg = new MessageInfo();
            msg.IsSuccess = true;
            msg.Message = "保存成功";

            return Json(msg, "text/html", JsonRequestBehavior.AllowGet);
        }       


        #region 私有方法

        private void CreateTree(TreeData curTreeData,DicCategory curDicCategory, List<DicCategory> list)
        {
            var tempList = list.Where(d => d.ParentId == curDicCategory.Id);
            if (tempList.Count() > 0)
            {
                curTreeData.iconCls = "tree-folder";
                curTreeData.state = "open";
                curTreeData.children = new List<TreeData>();
                foreach (DicCategory dicCategory in tempList)
                {
                    TreeData treeData = ConvertToTreeData(dicCategory);
                    curTreeData.children.Add(treeData);
                    CreateTree(treeData, dicCategory, list);
                }
            }
            else
            {
                curTreeData.iconCls = "tree-file";
                curTreeData.state = null;
                curTreeData.children = new List<TreeData>();
            }
        }

        private TreeData ConvertToTreeData(DicCategory dicCategory)
        {
            TreeData treeData = new TreeData();
            treeData.id = dicCategory.Id;
            treeData.text = dicCategory.Name;
            treeData.iconCls = "";
            return treeData;
        }

        #endregion
    }
}
